// -----------------------------
// Author: 		Dhaenens Gert	
// Creation: 	2/26/2015 5:58:30 PM				
// -----------------------------

#include "FileManager.h"

#include "FilePackage.h"

namespace Flow
{

	FileManager::FileManager()
	{
	}

	FileManager::~FileManager()
	{
		for (FilePackage* lpPackage : m_Packages)
			delete lpPackage;
	}

	Bool FileManager::LoadPackage(const tstring& name, const tstring& path)
	{
		string aname = string(name.begin(), name.end());
		string apath = string(path.begin(), path.end());
		Log::WriteLine("Loading file package \"%s\" (\"%s\").", aname.c_str(), apath.c_str());
		FilePackage* lpPackage = new FilePackage(name, path);
		if (!lpPackage->ReadAllFileInfo())
		{
			Log::WriteLine("Failed to read file info for package \"%s\".", aname.c_str());
			delete lpPackage;

			return false;
		}

		m_Packages.push_back(lpPackage);

		return true;
	}
	void FileManager::UnloadPackage(const tstring& name)
	{
		string aname = string(name.begin(), name.end());
		Log::WriteLine("Unloading file package \"%s\".", aname.c_str());
		
		auto searchFunc = [&name](FilePackage* lpPackage) -> Bool
		{
			if (lpPackage->GetName() == name)
				return true;
			return false;
		};
		auto foundIt = find_if(m_Packages.begin(), m_Packages.end(), searchFunc);
		ASSERT(foundIt != m_Packages.end());
		if (foundIt != m_Packages.end())
		{
			FilePackage* lpPackage = (*foundIt);
			m_Packages.erase(foundIt);
			delete lpPackage;
		}
	}
	void FileManager::UnloadPackage(FilePackage* lpPackage)
	{
		ASSERT(lpPackage);
		UnloadPackage(lpPackage->GetName());
	}

	FilePackage* FileManager::GetPackageByName(const tstring& name) const
	{
		auto searchFunc = [&name](FilePackage* lpPackage) -> Bool
		{
			if (lpPackage->GetName() == name)
				return true;
			return false;
		};
		auto foundIt = find_if(m_Packages.begin(), m_Packages.end(), searchFunc);
		ASSERT(foundIt != m_Packages.end());
		if (foundIt != m_Packages.end())
			return (*foundIt);
		return nullptr;
	}
	FilePackage* FileManager::GetPackageByPath(const tstring& path) const
	{
		auto searchFunc = [&path](FilePackage* lpPackage) -> Bool
		{
			if (lpPackage->GetPath() == path)
				return true;
			return false;
		};
		auto foundIt = find_if(m_Packages.begin(), m_Packages.end(), searchFunc);
		ASSERT(foundIt != m_Packages.end());
		if (foundIt != m_Packages.end())
			return (*foundIt);
		return nullptr;
	}
	FilePackage* FileManager::GetPackageByIndex(UInt32 idx) const
	{
		ASSERT(m_Packages.size() > idx);
		return m_Packages.at(idx);
	}
	FilePackage* FileManager::FindPackageForFile(const tstring& name) const
	{
		for (FilePackage* lpPackage : m_Packages)
		{
			if (lpPackage->ContainsFile(name))
				return lpPackage;
		}
		return nullptr;
	}

	UInt32 FileManager::GetPackageCount() const
	{
		return (UInt32) m_Packages.size();
	}
	Bool FileManager::DoesPackageExistByName(const tstring& name) const
	{
		auto searchFunc = [&name](FilePackage* lpPackage) -> Bool
		{
			if (lpPackage->GetName() == name)
				return true;
			return false;
		};
		auto foundIt = find_if(m_Packages.begin(), m_Packages.end(), searchFunc);
		return (foundIt != m_Packages.end());
	}
	Bool FileManager::DoesPackageExistByPath(const tstring& path) const
	{
		auto searchFunc = [&path](FilePackage* lpPackage) -> Bool
		{
			if (lpPackage->GetPath() == path)
				return true;
			return false;
		};
		auto foundIt = find_if(m_Packages.begin(), m_Packages.end(), searchFunc);
		return (foundIt != m_Packages.end());
	}
}